package patrol.mobile.com.mobilepatrol.persenter;


import android.util.Log;

import com.google.gson.Gson;

import java.util.List;

import okhttp3.MultipartBody;
import okhttp3.ResponseBody;
import patrol.mobile.com.mobilepatrol.api.ApiService;
import patrol.mobile.com.mobilepatrol.utils.Constants;
import patrol.mobile.com.mobilepatrol.utils.SPUtils;
import patrol.mobile.com.mobilepatrol.view.UploadImgView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Part;

/**
 * Created by shjshi on 2017/8/9.
 */

public class UploadPicsPresenter {
    private final ApiService apiService;
    private UploadImgView mView;
    List<MultipartBody.Part> partList;

    public UploadPicsPresenter(UploadImgView view, @Part List<MultipartBody.Part> partList) {
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(new Gson()))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl((String) SPUtils.get(Constants.current_ser, ""))
                .build();
        apiService = retrofit.create(ApiService.class);
        this.mView = view;
        this.partList = partList;
    }

    public void uploadPic() {
        Call<ResponseBody> call = apiService.uploadImages(partList);
// 用法和OkHttp的call如出一辙,
// 不同的是如果是Android系统回调方法执行在主线程
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response!=null){

                    Log.d("shj--",response.toString());
                    if (response.code()==200){
                        mView.upImgSuccess(response.code()+"");
                    }else{
                        mView.upImgError(response.code()+"");
                    }
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }
    public void uploadTrackImg() {
        Call<ResponseBody> call = apiService.uploadTrack(partList);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response!=null){

                    Log.d("shj--",response.body().toString());
                    if (response.code()==200){
                        mView.upImgSuccess(response.code()+"");
                    }else{
                        mView.upImgError(response.code()+"");
                    }
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                mView.upImgError(t.getMessage());
            }
        });
    }
    public void uploadTongzuImages() {
        Call<ResponseBody> call = apiService.uploadTongzuImages(partList);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response!=null){

                    Log.d("shj--",response.body().toString());
                    if (response.code()==200){
                        mView.upImgSuccess(response.code()+"");
                    }else{
                        mView.upImgError(response.code()+"");
                    }
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                mView.upImgError(t.getMessage());
            }
        });
    }
}
